package user.view;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.JScrollPane;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import user.dao.UserDao;
import user.model.Book;
import user.model.Borrow;
import user.model.User;

import java.awt.Font;
import java.util.List;
import java.util.Vector;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MyBook extends JInternalFrame {
	private JTable table;
	private UserDao userDao = new UserDao();
	private static String loginName = null;
	private JTextField bookNameTxt;
	private JTextField authorTxt;
	private JTextField btimeTxt;
	private JTextField rtimeTxt;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MyBook frame = new MyBook();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MyBook() {
		setClosable(true);
		setIconifiable(true);
		setTitle("我的书架");
		setBounds(100, 100, 1018, 704);
		
		JScrollPane scrollPane = new JScrollPane();
		
		JLabel lblNewLabel = new JLabel("书名");
		lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 25));
		
		bookNameTxt = new JTextField();
		bookNameTxt.setFont(new Font("宋体", Font.PLAIN, 25));
		bookNameTxt.setColumns(10);
		
		JLabel lblNewLabel_1 = new JLabel("作者");
		lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 25));
		
		authorTxt = new JTextField();
		authorTxt.setFont(new Font("宋体", Font.PLAIN, 25));
		authorTxt.setColumns(10);
		
		JLabel lblNewLabel_2 = new JLabel("借书日期");
		lblNewLabel_2.setFont(new Font("宋体", Font.PLAIN, 25));
		
		btimeTxt = new JTextField();
		btimeTxt.setFont(new Font("宋体", Font.PLAIN, 25));
		btimeTxt.setColumns(10);
		
		JButton btnNewButton = new JButton("归还");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int row = table.getSelectedRow();
				if(table.isRowSelected(row)==false) {
					JOptionPane.showMessageDialog(null, "请选择一行");
				}
				else {
					String x = JOptionPane.showInputDialog(null, "请输入密钥");
				if(x.equals("123456")) {
					ReturnBookActionPerformed(e);
				}
				else {
					JOptionPane.showMessageDialog(null, "密钥错误，归还失败！");
				}
				}
				
			}
		});
		btnNewButton.setFont(new Font("宋体", Font.PLAIN, 25));
		
		JButton btnNewButton_1 = new JButton("返回");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		btnNewButton_1.setFont(new Font("宋体", Font.PLAIN, 25));
		
		JLabel label = new JLabel("归还日期");
		label.setFont(new Font("宋体", Font.PLAIN, 25));
		
		rtimeTxt = new JTextField();
		rtimeTxt.setFont(new Font("宋体", Font.PLAIN, 25));
		rtimeTxt.setColumns(10);
		
		JButton btnNewButton_2 = new JButton("续借");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int row = table.getSelectedRow();
				if(table.isRowSelected(row)==false) {
					JOptionPane.showMessageDialog(null, "请选择一行");
				}
				else {
					RenewBookActionPerformed(e);
				}
				
			}
		});
		btnNewButton_2.setFont(new Font("宋体", Font.PLAIN, 25));
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 512, Short.MAX_VALUE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(label, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblNewLabel_2)
								.addComponent(lblNewLabel_1)
								.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 72, GroupLayout.PREFERRED_SIZE))
							.addGroup(groupLayout.createSequentialGroup()
								.addGap(18)
								.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
									.addComponent(bookNameTxt, GroupLayout.PREFERRED_SIZE, 229, GroupLayout.PREFERRED_SIZE)
									.addComponent(authorTxt, 229, 229, 229)
									.addComponent(btimeTxt, 229, 229, 229)
									.addComponent(rtimeTxt, GroupLayout.PREFERRED_SIZE, 229, GroupLayout.PREFERRED_SIZE))))
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(btnNewButton_2)
							.addGap(42)
							.addComponent(btnNewButton)
							.addGap(39)
							.addComponent(btnNewButton_1)))
					.addContainerGap())
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(146)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 42, GroupLayout.PREFERRED_SIZE)
						.addComponent(bookNameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(49)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_1)
						.addComponent(authorTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(57)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(lblNewLabel_2)
						.addComponent(btimeTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(54)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(3)
							.addComponent(label, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE))
						.addComponent(rtimeTxt, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE))
					.addGap(114)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnNewButton_1, GroupLayout.PREFERRED_SIZE, 57, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnNewButton, GroupLayout.DEFAULT_SIZE, 56, Short.MAX_VALUE)
						.addComponent(btnNewButton_2, GroupLayout.PREFERRED_SIZE, 53, GroupLayout.PREFERRED_SIZE))
					.addGap(66))
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(40)
					.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 596, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(34, Short.MAX_VALUE))
		);
		
		table = new JTable();
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				MyBookMousePressed(e);
			}
		});
		table.setFont(new Font("宋体", Font.PLAIN, 18));
		table.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"\u4E66\u7C4D\u7F16\u53F7", "\u4E66\u7C4D\u540D\u79F0", "\u4F5C\u8005", "\u501F\u4E66\u65E5\u671F", "\u5E94\u8FD8\u65E5\u671F"
			}
		));
		table.getColumnModel().getColumn(0).setPreferredWidth(80);
		table.getColumnModel().getColumn(0).setMinWidth(40);
		table.getColumnModel().getColumn(1).setPreferredWidth(80);
		table.getColumnModel().getColumn(1).setMinWidth(40);
		table.getColumnModel().getColumn(2).setPreferredWidth(80);
		table.getColumnModel().getColumn(2).setMinWidth(40);
		table.getColumnModel().getColumn(4).setPreferredWidth(90);
		table.getColumnModel().getColumn(4).setMinWidth(40);
		scrollPane.setViewportView(table);
		getContentPane().setLayout(groupLayout);
		fillTable();
	}
	
	protected void RenewBookActionPerformed(ActionEvent e) {
		String bookName = this.bookNameTxt.getText();
		Book book = userDao.selectBookId(bookName);
		int bookid = book.getBookId();
		loginName = LoginFrame.userName();
		User u = userDao.selectUname(loginName);
		String userId = u.getId();
		Borrow borrow = userDao.selectTimes(userId,bookid);
		if(borrow.getTimes()==0) {
			int j = userDao.renewBook(userId,bookid);
			if(j>0) {
				userDao.renewStime(userId,bookid);
				JOptionPane.showMessageDialog(null, "第一次续借成功，还有一次续借机会！");
				fillTable();
				reset();
			}
			else {
				JOptionPane.showMessageDialog(null, "续借失败，请重试！");
			}
		}
		if(borrow.getTimes()==1) {
			int j = userDao.renewBooktwo(userId, bookid);
			if(j>0) {
				userDao.renewStime(userId,bookid);
				JOptionPane.showMessageDialog(null, "第二次续借成功，两次续借机会已用完！");
				fillTable();
				reset();
			}
			else {
				JOptionPane.showMessageDialog(null, "续借失败，请重试！");
			}
		}
		if(borrow.getTimes()==2) {
			JOptionPane.showMessageDialog(null,"续借次数已用完，续借失败！" );
		}
		
	}

	protected void ReturnBookActionPerformed(ActionEvent e) {
		String bookName = this.bookNameTxt.getText();
		Book book = userDao.selectBookId(bookName);
		int bookid = book.getBookId();
		loginName = LoginFrame.userName();
		User u = userDao.selectUname(loginName);
		String userId = u.getId();
		int i = userDao.updateBookLend(bookid);
		if(i>0) {
			int j = userDao.updateRtime(userId,bookid);
			if(j>0) {
				JOptionPane.showMessageDialog(null, "归还成功");
				fillTable();
				reset();
				
			}else {
				JOptionPane.showMessageDialog(null, "归还失败");
			}
		}else {
			JOptionPane.showMessageDialog(null, "归还失败");
		}
	}

	/**
	 * 点击表格行数据显示事件处理
	 * @param e
	 */
	protected void MyBookMousePressed(MouseEvent e) {
		int row = table.getSelectedRow();//获得行号
		bookNameTxt.setText((String) table.getValueAt(row, 1));//获取第一列信息
		authorTxt.setText((String) table.getValueAt(row, 2));
		btimeTxt.setText(String.valueOf( table.getValueAt(row, 3)));
		rtimeTxt.setText(String.valueOf( table.getValueAt(row, 4)));
	}

	/**
	 * 初始化表格
	 */
	public void fillTable() {
		DefaultTableModel dtm = (DefaultTableModel) table.getModel();
	    dtm.setRowCount(0);//设置成0行
	    //查询出书籍
	    loginName = LoginFrame.userName();
		User u = userDao.selectUname(loginName);
	    List<Book> bookList = userDao.listBook(u.getId());
	    for(Book book:bookList) {
	    	Vector v = new Vector<>();
	    	v.add(dtm.getRowCount()+1);
	    	v.add(book.getBookName());
	    	v.add(book.getAuthor());
	    	v.add(book.getBtime());
	    	v.add(book.getRtime());
	    	dtm.addRow(v);
	    }
	}
	public void reset() {
		this.bookNameTxt.setText("");
		this.authorTxt.setText("");
		this.btimeTxt.setText("");
		this.rtimeTxt.setText("");
	}
}
